package com.ms.oschina.viewholder;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ms.oschina.R;
import com.ms.oschina.base.BaseViewHolder;
import com.ms.oschina.bean.Tweet;
import com.ms.oschina.util.RichTextHelper;
import com.ms.oschina.util.StringUtils;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.view.CircleImageView;
import com.ms.oschina.view.TextViewFixTouchConsume;
import com.squareup.picasso.Picasso;

public class TweetDetailHeadViewHolder extends BaseViewHolder<Tweet> implements
		OnClickListener {

	private CircleImageView civTweetdetailUserAvatar;
	private TextView tvTweetUserName;
	private TextView tvTweetdetailTweetTime;
	private TextView tvTweetdetailAppclient;
	private WebView wvTweetdetailTweetDetailContent;
	private RelativeLayout rlTweetdetailLike;
	private ImageView imageView2;
	private TextView rlTweetdetailCommentCount;
	private TextViewFixTouchConsume tvTweetdetaillikeInfo;
	private TextViewFixTouchConsume wvTweetdetailLikeItem;
	private ImageView ivTweetImg;
	private Tweet tweet;

	private void findViews(View view) {
		civTweetdetailUserAvatar = (CircleImageView) view
				.findViewById(R.id.civ_aboutme_userAvatar);
		tvTweetUserName = (TextView) view.findViewById(R.id.iv_event_title);
		tvTweetdetailTweetTime = (TextView) view
				.findViewById(R.id.iv_event_time);
		tvTweetdetailAppclient = (TextView) view
				.findViewById(R.id.tv_aboutme_time);
		wvTweetdetailTweetDetailContent = (WebView) view
				.findViewById(R.id.wv_tweetdetail_tweetDetailContent);
		rlTweetdetailLike = (RelativeLayout) view
				.findViewById(R.id.rl_tweet_like);
		imageView2 = (ImageView) view.findViewById(R.id.iv_tweet_like);
		rlTweetdetailCommentCount = (TextView) view
				.findViewById(R.id.rl_tweet_commentCount);
		tvTweetdetaillikeInfo = (TextViewFixTouchConsume) view
				.findViewById(R.id.tv_aboutme_mySeedInfo);
		ivTweetImg = (ImageView) view.findViewById(R.id.iv_tweet_tweetImg);
	}

	@Override
	public View initView() {
		View view = UiUtils.inflate(R.layout.tweetdetail_headview);
		findViews(view);
		return view;
	}

	@Override
	@JavascriptInterface
	public void setData(int position, Tweet tweet) {
		this.tweet = tweet;
		ivTweetImg.setOnClickListener(this);
		tvTweetUserName.setText(tweet.getAuthor());
		String portrait = tweet.getPortrait();

		if (!TextUtils.isEmpty(portrait)) {
			Picasso.with(UiUtils.getContext()).load(portrait)
					.placeholder(R.drawable.widget_dface)
					.into(civTweetdetailUserAvatar);
		}

		loadSmallImg(tweet);

		rlTweetdetailCommentCount.setText(tweet.getCommentCount());

		wvTweetdetailTweetDetailContent.loadDataWithBaseURL("",
				tweet.getBody(), "text/html", "utf-8", "");

		tvTweetdetaillikeInfo.setVisibility(View.VISIBLE);
		int green = UiUtils.getContext().getResources().getColor(R.color.green);
		RichTextHelper.showLike(tvTweetdetaillikeInfo, 8, tweet, green);
	}

	public void loadSmallImg(Tweet tweet) {
		String img = tweet.getImgSmall();
		if (TextUtils.isEmpty(img)) {
			ivTweetImg.setVisibility(View.GONE);
		} else {
			ivTweetImg.setVisibility(View.VISIBLE);
			Picasso.with(UiUtils.getContext()).load(img).placeholder(R.drawable.img_load).into(ivTweetImg);
		}
	}

	@Override
	public void onClick(View v) {
		String imgBig = tweet.getImgBig();
		if (!StringUtils.isEmpty(imgBig)) {
			String imgSmall = tweet.getImgSmall();
			UiUtils.showImagePreview(UiUtils.getContext(),imgSmall, imgBig);
		}
	}

}
